package com.ylr.template;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;

/**
 * @author ylr
 * @version 1.0.0
 * @ClassName ApplicationUtil.java
 * @Description 获取bean的工具类
 * @createTime 2022年01月08日 14:04:00
 */

@Component
public class ApplicationUtil implements ApplicationContextAware {
    /*
    *
    *获取ApplicationContext的几种方式
    *
    * */
    //1直接注入
    @Resource
    private ApplicationContext ctx;



    //2实现ApplicationContextAware接口
    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        ApplicationUtil.applicationContext = ctx;
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

}
